package com.yunda.rfidutils;

import androidx.appcompat.app.AppCompatActivity;

import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.yunda.rfid_library.NFCReadFragment;
import com.yunda.rfid_library.NFCUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Random;

/**
 * RFID相关帮助类
 * UHF与NFC
 */
public class MainActivity extends AppCompatActivity {
    private Boolean Read=true;
    private NFCReadFragment mNfcReadFragment;
    protected NfcAdapter mNfcAdapter;
    private PendingIntent mPendingIntent;
    private TextView tv;
    private CheckBox cbRead;
    private CheckBox cbWrite;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        NFCUtils.INSTANCE.CheckNFC(MainActivity.this);
        cbRead=findViewById(R.id.cbRead);
        cbWrite=findViewById(R.id.cbWrite);
        cbRead.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Read=true;
                    cbWrite.setChecked(false);
                }else {
                    Read=false;
                    cbWrite.setChecked(true);
                }
            }
        });


        cbWrite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Read=false;
                    cbRead.setChecked(false);
                }else {
                    Read=true;
                    cbRead.setChecked(true);
                }
            }
        });

        findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Boolean aBoolean = NFCUtils.INSTANCE.CheckNFC(MainActivity.this);
//                if (aBoolean){
////                    Toast.makeText(MainActivity.this,"可以使用NFC",Toast.LENGTH_SHORT).show();
//                }else {
////                    Toast.makeText(MainActivity.this,"不可以使用NFC",Toast.LENGTH_SHORT).show();
//                }

            }
        });
//        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
//        IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
//        IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
//        IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
//        IntentFilter[] nfcIntentFilter = new IntentFilter[]{techDetected,tagDetected,ndefDetected};
//
//        PendingIntent pendingIntent = PendingIntent.getActivity(
//                this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
//        if(mNfcAdapter!= null)
//            mNfcAdapter.enableForegroundDispatch(this, pendingIntent, nfcIntentFilter, null);
      NFCUtils.INSTANCE.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
//        if(mNfcAdapter!= null)
//            mNfcAdapter.disableForegroundDispatch(this);
    NFCUtils.INSTANCE.onPause();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        try {
            NFCUtils.INSTANCE.onNewIntent(intent,Read);
        } catch (IOException e) {
            e.printStackTrace();
        }


        if(Read){

            String s = NFCUtils.INSTANCE.getReadInfo();
            Log.e("读取标签",s);
            Toast.makeText(MainActivity.this,"读取标签："+s,Toast.LENGTH_SHORT).show();
        }else {
            String info=new Random().nextInt(10000)+"";
            try {
                Boolean aBoolean = NFCUtils.INSTANCE.WriteNFC(info);
                if(aBoolean){
                    Log.e("写入标签","写入标签成功"+info);
                    Toast.makeText(MainActivity.this,"写入标签成功："+info,Toast.LENGTH_SHORT).show();
                }else {
                    Log.e("写入标签","写入标签失败");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

//        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
//        Log.e("NFC", "1111111111111");
//        Log.d("NFC", "onNewIntent: " + intent.getAction());
//
//        if (tag != null) {
//            Toast.makeText(this, getString(R.string.message_tag_detected), Toast.LENGTH_SHORT).show();
//            Ndef ndef = Ndef.get(tag);
//
//            try {
//                ndef.connect();
//                NdefMessage ndefMessage = ndef.getNdefMessage();
//
//                if(Read){
//                    String message = new String(ndefMessage.getRecords()[0].getPayload());
//                    Log.e("readFromNFC", "readFromNFC: "+message);
//                    Toast.makeText(MainActivity.this,"读取数据："+message,Toast.LENGTH_SHORT).show();
//                }else {
//                    try {
//                        String message=new Random().nextInt(10000)+"";
//                        NdefRecord mimeRecord = NdefRecord.createMime("text/plain", message.getBytes(Charset.forName("US-ASCII")));
//                        ndef.writeNdefMessage(new NdefMessage(mimeRecord));
//                        Toast.makeText(MainActivity.this,"标签写入成功！"+message,Toast.LENGTH_SHORT).show();
//                    } catch (FormatException | IOException e) {
//                        e.printStackTrace();
//                        Toast.makeText(MainActivity.this,"标签写入失败！",Toast.LENGTH_SHORT).show();
//                    }
//
//                }
//
//                ndef.close();
//
//            } catch (IOException | FormatException e) {
//                e.printStackTrace();
//
//            }
//
//        }
    }
}
